package com.lxhll.personnel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxhll.personnel.core.P;
import com.lxhll.personnel.core.ResponseData;
import com.lxhll.personnel.entity.Department;
import com.lxhll.personnel.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Map;



/**
 * (Department)表控制层
 *
 * @author makejava
 * @since 2021-07-17 14:59:28
 */
@RestController
@RequestMapping("department")
@Slf4j
public class DepartmentController {
    /**
     * 服务对象
     */
    @Autowired
    private DepartmentService departmentService;


    /**
     * 查询部门所有下员工，分页
     */
    @PostMapping("/list")
    public ResponseData list(@RequestBody Map<String ,Object> params){
        //提取查询参数与分页
        log.info("查询参数:"+params);
        //构建wrapper
        QueryWrapper<Department> wrapper=new QueryWrapper<Department>();
        //查询部门列表,可以按照部门编号，名称，类型查询
        Integer id=(Integer) params.get("id");
        String name=(String)params.get("name");
        String type=(String)params.get("type");
        if(id!=null)
            wrapper.like("id",id);
        if(StringUtils.isNotBlank(name))
            wrapper.like("name",name);
        if(StringUtils.isNotBlank(type))
            wrapper.like("type",type);

        IPage<Department> departmentIPage=departmentService.page(P.page(params),wrapper.orderByDesc("create_date"));
        //返回
        return ResponseData.ok(departmentIPage);
    }

    /**
     * 获取部门列表，作为职工操作时前台下拉框
     * @return
     */
    @GetMapping("/droplist")
    public ResponseData droplist(){
        return ResponseData.ok(departmentService.list());
    }


    /**
     * 查询单个部门信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public ResponseData info(@PathVariable("id") Integer id){
		Department department = departmentService.getDepartmentById(id);
        return ResponseData.ok(department);
    }

    /**
     * 删除部门，可以批量.如果该部门下有员工，则不能删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseData deleteDepartment(@RequestBody Integer[] ids){
        log.info("删除部门id"+ Arrays.toString(ids));

        boolean res=departmentService.removeDepartmentByIds(ids);
        if(!res){
            return ResponseData.error("部门下有所属员工，不能删除！");
        }
        return ResponseData.ok();
    }

    /**
     * 部门信息修改
     * @param department
     * @return
     */
    @PutMapping(value="/update")
    public ResponseData update(@Validated  @RequestBody Department department) {
        departmentService.updateDepartmentById(department);
        return ResponseData.ok();
    }
    
        /**
     * 增加部门
     */
    @PostMapping("/save")
    public ResponseData add(@Validated @RequestBody Department department){
        department.setCreateDate(LocalDate.now());
        departmentService.save(department);
        return ResponseData.ok();
    }
}

